package org.pool.util;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.common.util.JDBCUtil;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * DBCP连接池的工具类
 *
 * @author yexi
 */
public final class DBCPUtil {
    private DBCPUtil() {
    }

    private static DataSource dataSource = null;

    /**
     * 通过工厂类创建 DataSource 的话，properties文件就必须按照其规范来书写才可以，否则会取不到
     * @see BasicDataSourceFactory#createDataSource(Properties)
     */
    static {
        try {
            Properties properties = JDBCUtil.getPropertiesConnPool();
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
